#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "ai.h"
#include "board.h"
#include "fread.h"
#include "utilities.h"
#include "constants.h"

void minimax()
{
	srand(time(0));

	int depth = 6;
	int move, child;
	node * n = pop_node();
	n -> g = pop_grid();
    init_test_grid(n -> g);

    grid * temp;
    temp = pop_grid();
    copy_into(n -> g, temp);

    int i;

	while(TRUE)
	{
        print_grid(temp, 5, 1);
		n -> alpha = LOSE;
		n -> beta = GOOD;
		max_alphabeta(depth, n);
		move = solve(n, &max);
		if (move == -1)
		{
			printf("You lose!\n");
			break;
		}
		printf("Suggested move: %d\n", n -> children[move] -> last_move);
        direction_func[n -> children[move] -> last_move](temp);
		set_new_root(&n, n -> children[move]);

		print_grid(temp, 5, 1);
		//min_minimax(1, n);
		child = rand() % n -> num_children;
		move = n -> children[child] -> last_move;
        printf("move: %d\n", move);
        temp -> array[move / NUM_COMPUTER_CHOICES] =
            computer_choices[move % NUM_COMPUTER_CHOICES];
		set_new_root(&n, n -> children[child]);
	}
	
    push_grid(temp);
	push_node(n);
    cleanup_unused_grids();
    cleanup_unused_nodes();
}

int main(int argc, char **argv)
{
	minimax();
	return 0;
}
